package com.sxg.controller;

import com.sxg.convert.SxgGoodsCollectionConvert;
import com.sxg.entity.SxgGoodsCollectionEntity;
import com.sxg.query.SxgGoodsCollectionQuery;
import com.sxg.utils.code.AjaxResult;
import com.sxg.vo.SxgGoodsCollectionVO;
import com.sxg.vo.req.CollectionReqVo;
import com.sxg.vo.req.GoodsCollectionReqVo;
import com.sxg.vo.resp.CollectionRespVo;
import lombok.AllArgsConstructor;
import com.sxg.page.PageResult;
import com.sxg.utils.code.Result;
import com.sxg.service.SxgGoodsCollectionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;


/**
 * 商品收藏
 *
 * @author 浪漫写尽歌谱 3298751420@qq.com
 * @since 1.0.0 2024-03-07
 */
@RestController
@RequestMapping("sxg/sxg_goods_collection")
@AllArgsConstructor
public class SxgGoodsCollectionController {

    private final SxgGoodsCollectionService sxgGoodsCollectionService;

    @GetMapping("page")
    public AjaxResult page(@Valid SxgGoodsCollectionQuery query) {
        PageResult<CollectionRespVo> page = sxgGoodsCollectionService.page(query);

        return AjaxResult.success(page.getList()).put("total", page.getTotal());
    }

    @GetMapping("{id}")
    @PreAuthorize("hasAuthority('sxg:sxg_goods_collection:info')")
    public Result<SxgGoodsCollectionVO> get(@PathVariable("id") Long id) {
        SxgGoodsCollectionEntity entity = sxgGoodsCollectionService.getById(id);
        return Result.ok(SxgGoodsCollectionConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    public AjaxResult save(@RequestBody @Valid GoodsCollectionReqVo vo) {
        return sxgGoodsCollectionService.saveByUserId(vo);
    }

    @PutMapping
    @PreAuthorize("hasAuthority('sxg:sxg_goods_collection:update')")
    public Result<String> update(@RequestBody @Valid SxgGoodsCollectionVO vo) {
        sxgGoodsCollectionService.update(vo);

        return Result.ok();
    }

    @DeleteMapping
    public AjaxResult delete(@RequestBody CollectionReqVo ids) {
        return sxgGoodsCollectionService.delete(ids.getIds());
    }

}